package com.zeng.createmd.restfulmodule.util;

import com.zeng.createmd.restfulmodule.constant.DataSaveLocation;
import com.zeng.createmd.restfulmodule.itemCache.ItemCache;
import com.zeng.createmd.servicemodule.vo.DatabaseVo;
import com.zeng.createmd.servicemodule.vo.ItemVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

@Slf4j
@Component
public class ItemUtil {
    private File data = new File(DataSaveLocation.LOCATION, "data");

    private List<ItemVo> itemVos;

    public ItemUtil() {
        itemVos = get();
    }

    public void init() {
        if (itemVos != null) {
            SerializableUtil.serialize(data, itemVos);
        }
    }

    public List<ItemVo> get() {

        if (!data.exists()) {
            return new ArrayList<>();
        }
        FileInputStream fileInputStream = null;
        ObjectInputStream objectInputStream = null;
        Object o = null;
        try {
            fileInputStream = new FileInputStream(data);
            objectInputStream = new ObjectInputStream(fileInputStream);
            o = objectInputStream.readObject();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            close(fileInputStream, objectInputStream);

        }

        if (o instanceof List) {
            return (List<ItemVo>) o;
        }
        return new ArrayList<>();
    }

    public void add(ItemVo itemVo) {
        this.itemVos.add(itemVo);
        init();

    }

    public void updateItemVo(ItemVo itemVo) {
        ItemVo detail = detail(itemVo.getCode());
        if (itemVo == detail) {
            init();
            return;
        }
        throw new RuntimeException("更新失败，请查看代码");
    }

    public ItemVo detail(String code) {
        if (code == null) {
            return null;
        }
        List<ItemVo> itemVos = this.itemVos;
        Stream<ItemVo> itemVoStream = itemVos.stream().filter(e -> code.equals(e.getCode()));
        return itemVoStream.findFirst().orElse(null);
    }

    public DatabaseVo getDatabaseVo() {
        ItemVo currentItem = ItemCache.currentItem;
        if (currentItem == null) {
            throw new RuntimeException("尚未选择项目");
        }
        return (DatabaseVo)currentItem.getInfo().get("database");
    }

    public void addDatabaseV(DatabaseVo databaseVo) {
        ItemVo currentItem = ItemCache.currentItem;
        if (currentItem == null) {
            throw new RuntimeException("尚未选择项目");
        }
        currentItem.getInfo().put("database", databaseVo);
        updateItemVo(currentItem);
    }

    public static void close(Closeable... closeables) {
        for (Closeable closeable : closeables) {
            if (closeable != null) {
                try {
                    closeable.close();
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
    }
}
